package classes;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Produit.
 * 
 * @see classes.Produit
 * @author MyEclipse Persistence Tools
 */

public class ProduitDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ProduitDAO.class);

	// property constants
	public static final String NOM = "nom";

	public static final String CODE_BARRE = "codeBarre";

	public static final String PRIX_HT = "prixHt";

	public static final String MARGE = "marge";

	public static final String SEUIL_MINI_RAYON = "seuilMiniRayon";

	public static final String SEUIL_MINI_STOCK = "seuilMiniStock";

	public static final String NOMBRE_STOCK = "nombreStock";

	public static final String NOMBRE_RAYON = "nombreRayon";

	public static final String ID_RAYON = "idRayon";

	public void save(Produit transientInstance) {
		log.debug("saving Produit instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Produit persistentInstance) {
		log.debug("deleting Produit instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Produit findById(java.lang.Integer id) {
		log.debug("getting Produit instance with id: " + id);
		try {
			Produit instance = (Produit) getSession()
					.get("classes.Produit", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Produit instance) {
		log.debug("finding Produit instance by example");
		try {
			List results = getSession().createCriteria("classes.Produit").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Produit instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Produit as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByNom(Object nom) {
		return findByProperty(NOM, nom);
	}

	public List findByCodeBarre(Object codeBarre) {
		return findByProperty(CODE_BARRE, codeBarre);
	}

	public List findByPrixHt(Object prixHt) {
		return findByProperty(PRIX_HT, prixHt);
	}

	public List findByMarge(Object marge) {
		return findByProperty(MARGE, marge);
	}

	public List findBySeuilMiniRayon(Object seuilMiniRayon) {
		return findByProperty(SEUIL_MINI_RAYON, seuilMiniRayon);
	}

	public List findBySeuilMiniStock(Object seuilMiniStock) {
		return findByProperty(SEUIL_MINI_STOCK, seuilMiniStock);
	}

	public List findByNombreStock(Object nombreStock) {
		return findByProperty(NOMBRE_STOCK, nombreStock);
	}

	public List findByNombreRayon(Object nombreRayon) {
		return findByProperty(NOMBRE_RAYON, nombreRayon);
	}

	public List findByIdRayon(Object idRayon) {
		return findByProperty(ID_RAYON, idRayon);
	}

	public List findAll() {
		log.debug("finding all Produit instances");
		try {
			String queryString = "from Produit";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Produit merge(Produit detachedInstance) {
		log.debug("merging Produit instance");
		try {
			Produit result = (Produit) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Produit instance) {
		log.debug("attaching dirty Produit instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Produit instance) {
		log.debug("attaching clean Produit instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}